package com.project.web.admin.controller.core;

import com.github.pagehelper.PageInfo;
import com.project.model.core.StdCodeType;
import com.project.web.admin.controller.common.BaseController;
import com.project.web.admin.service.core.StdCodeTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * 标准编码类型controller
 */
@Controller("stdCodeTypeController")
@RequestMapping("/admin/stdCodeType")
public class StdCodeTypeController extends BaseController {

    private static final long serialVersionUID = 9155517391066753558L;

    @Resource(name = "stdCodeTypeServiceImpl")
    private StdCodeTypeService stdCodeTypeService;

    /**
     * 编码类型列表
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, StdCodeType stdCodeType, Integer pageNumber, Integer pageSize) {
        PageInfo<StdCodeType> stdCodeTypePageInfo = new PageInfo<>();
        stdCodeTypePageInfo.setPageSize(pageSize);
        stdCodeTypePageInfo.setPageNum(pageNumber);
        model.addAttribute("page", stdCodeTypeService.selectList(stdCodeType, stdCodeTypePageInfo));
        model.addAttribute("codeType", stdCodeType.getCodeType());
        model.addAttribute("codeTypeName", stdCodeType.getCodeTypeName());

        return "/intfc/core/stdCodeType/list.ftl";
    }


    /**
     * 检查编码类型是否存在
     *
     * @return
     */
    @RequestMapping(value = "/exist", method = RequestMethod.GET)
    @ResponseBody
    public boolean checkCodeTypeIsExist(String codeType, Long id) {
        StdCodeType stdCodeType = stdCodeTypeService.getByCodeType(codeType);
        if (stdCodeType == null) {
            return true;
        } else {
            return id == null ? false : stdCodeType.getId().equals(id);
        }
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        return "/intfc/core/stdCodeType/add.ftl";
    }

    /**
     * 保存
     *
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(StdCodeType stdCodeType, Model model) {
        stdCodeType.setCodeType(stdCodeType.getCodeType().toUpperCase());
        stdCodeTypeService.save(stdCodeType);
        addLayerFlashMessage(model);
        return LAYER_FORWARD_VIEW;
    }

    /**
     * 编辑
     *
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, Model model, RedirectAttributes redirectAttributes) {

        StdCodeType stdCodeType = stdCodeTypeService.getById(id);
        model.addAttribute("stdCodeType", stdCodeType);

        return "/intfc/core/stdCodeType/edit.ftl";
    }

    /**
     * 更新
     *
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(StdCodeType stdCodeType, Model model) {

        stdCodeType.setCodeType(stdCodeType.getCodeType().toUpperCase());
        stdCodeTypeService.update(stdCodeType);
        addLayerFlashMessage(model);

        return LAYER_FORWARD_VIEW;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(Long id, RedirectAttributes redirectAttributes) {
        stdCodeTypeService.delete(id);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }
}
